const util = require('../../../../../utils/util.js');
const api = require('../../../../../config/api.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    albumAnnouncement: '',// 专辑公告
    albumAnnouncementLength: 0,//专辑公告人数
    albumEditorPeopleNumber: '',// 专辑参与人数
    albumEditorPrice: 0,//总价格
    price : 0,// 均价
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    // 初始化信息
    _this.loadInfo();
    // 初始化编辑价格
    _this.albumEditorPrice();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 专辑公告
   */
  albumAnnouncementInput: function(e){
    var _this = this;
    var albumAnnouncement = e.detail.value;
    _this.setData({
      albumAnnouncement: albumAnnouncement,
      albumAnnouncementLength: albumAnnouncement.length
    })
  },

  /**
   * 专辑人数
   */
  albumEditorPeopleNumberInput: function (e) {
    var _this = this;
    var albumEditorPrice = _this.data.albumEditorPrice;
    var albumEditorPeopleNumber = e.detail.value;
    var price = (albumEditorPrice / albumEditorPeopleNumber).toFixed(2);
    _this.setData({
      albumEditorPeopleNumber: albumEditorPeopleNumber,
      price: price
    })
  },

  /**
   * 确定按钮点击事件
   */
  success: function () {
    var _this = this;
    var pages = getCurrentPages();
    var albumAnnouncement = _this.data.albumAnnouncement;
    var albumEditorPeopleNumber = _this.data.albumEditorPeopleNumber;
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      albumAnnouncement: albumAnnouncement,
      albumEditorPeopleNumber: albumEditorPeopleNumber
    });
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 获取专辑编辑的总费用
   */
  albumEditorPrice: function(){
    var _this = this;
    util.post(api.Home_AlbumEditorPriceUrl).then(function (res) {
      if (res.code == 200) {
        var albumEditorPrice = res.result.albumEditorPrice;
        _this.setData({
          albumEditorPrice: albumEditorPrice,
          price: albumEditorPrice
        })
      }
    })
  },

  /**
   * 初始化信息
   */
  loadInfo: function(){
    var _this = this;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    var albumAnnouncement = prevPage.data.albumAnnouncement;
    var albumEditorPeopleNumber = prevPage.data.albumEditorPeopleNumber;
    console.log(albumAnnouncement);
    _this.setData({
      albumAnnouncement: albumAnnouncement,
      albumEditorPeopleNumber: albumEditorPeopleNumber
    })

  }
})